//package com.siruier.boss.ui.tool.image.select.service.intercept
//
//import android.net.Uri
//import cn.hutool.core.util.RandomUtil
//import com.alibaba.sdk.android.oss.ClientConfiguration
//import com.alibaba.sdk.android.oss.OSS
//import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken
//import com.alibaba.sdk.android.oss.common.utils.DateUtil
//import com.alibaba.sdk.android.oss.model.PutObjectRequest
//import com.siruier.boss.ui.tool.image.select.domain.ImageSelectBean
//import com.siruier.boss.ui.tool.image.select.service.ImageSelectIntercept
//import com.siruier.boss.ui.tool.image.select.service.ImageUploadUBuilder
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.withContext
//
//
//object AliYunIntercept : ImageSelectIntercept {
//    private const val ENDPOINT: String = "http://oss-cn-hangzhou.aliyuncs.com"
//    private const val bucketName: String = ""
//    private val CONF: ClientConfiguration = ClientConfiguration()
//    private var OSS: OSS? = null
//    private var cachedToken: OSSFederationToken? = null
//
//    init {
//        // 连接超时，默认15秒。
//        CONF.connectionTimeout = 15 * 1000
//        // socket超时，默认15秒。
//        CONF.socketTimeout = 15 * 1000
//        // 最大并发请求书，默认5个。
//        CONF.maxConcurrentRequest = 5
//        // 失败后最大重试次数，默认2次。
//        CONF.maxErrorRetry = 2
//    }
//
//    private suspend fun getAliYunOss(): OSS {
//        if (OSS == null || cachedToken == null ||
//            DateUtil.getFixedSkewedTimeMillis() / 1000 > cachedToken!!.expiration - 5 * 60
//        ) {
////        LOGIN_API_SERVICE.appInit()
//        }
//        return OSS!!
//    }
//
//    override suspend fun intercept(
//        config: ImageUploadUBuilder,
//        localMedias: MutableList<ImageSelectBean>?
//    ): MutableList<ImageSelectBean>? {
//        localMedias?.forEach { localMedia ->
//            val objectKey = objectKey()
//            val put: PutObjectRequest = if (localMedia.path.startsWith("content://")) {
//                PutObjectRequest(bucketName, objectKey, Uri.parse(localMedia.path))
//            } else {
//                PutObjectRequest(bucketName, objectKey, localMedia.path)
//            }
//            localMedia.url = withContext(Dispatchers.IO) {
//                getAliYunOss().run {
//                    putObject(put)
//                    presignPublicObjectURL(bucketName, objectKey)
//                }
//            }
//        }
//        return localMedias
//    }
//
//    private fun objectKey() = RandomUtil.randomString(8) + System.currentTimeMillis() + ".png"
//}